<?php if(!$B['error']):?>
<style>
#option_list li{width: 540px; border-bottom: 1px dashed gray;}
#option_list li>.remove_option{float:right;}
#option_list li>span{width: 490px; overflow: hidden; display: block;}
</style>
<div class="q_box">
	<h3>编辑投票</h3>
	<hr/>
	<form id="add_vote_form" class="form-horizontal" action="<?php echo BASE_URL;?>/?c=index&a=index" method="post" onsubmit="return false;">
		<input type="hidden" name="id" value="<?php echo $B['vote']->v('Id');?>" />
		<div class="control-group">
			<label class="control-label" for="inputName">投票名称</label>
			<div class="controls">
				<input type="text" id="inputName" class="input-xxlarge" value="<?php echo t::out($B['vote']->v('Name'));?>" placeholder="5~30个字符">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputDescription">投票描述</label>
			<div class="controls">
				<textarea id="inputDescription" name="optionnum" rows="3" class="input-xlarge" style="resize: none;"><?php echo t::out($B['vote']->v('Description'));?></textarea>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputAddoption">选项内容</label>			
			<div class="controls">
				<div class="input-append" id="add_option_box">
					<input type="text" id="inputAddoption" class="input-xxlarge" placeholder="输入选项内容后点击添加">
					<button class="btn" id="add_option_btn" type="button">添加</button>
				</div>
				<ol id="option_list" style="padding:10px;">
					<?php $options = json_decode($B['vote']->v('Options'),true);
					foreach($options as $k=>$v):?>
					<li>
						<a href="javascript:;" class="remove_option">删除</a>
						<span><?php echo t::out($v);?></span>
					</li>
					<?php endforeach;?>
				</ol>				
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="optionsRadios1">投票类型</label>
			<div class="controls">
				<label class="radio">
					<input type="radio" name="optionsRadios" id="optionsRadios1" value="0" <?php echo $B['vote']->v('VoteType')==0?'checked':'';?>>
					单选（投票者只能投给1个选项）
				</label>
				<label class="radio">
					<input type="radio" name="optionsRadios" id="optionsRadios2" value="1"<?php echo $B['vote']->v('VoteType')==1?'checked':'';?>>
					多选（投票者<font color="red">必须</font>投给n个选项）
				</label>
				<label class="radio">
					<input type="radio" name="optionsRadios" id="optionsRadios2" value="2"<?php echo $B['vote']->v('VoteType')==2?'checked':'';?>>
					多选（投票者<font color="red">可以</font>投给1~n个选项）
				</label>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputPicklimit">多选项数</label>
			<div class="controls">
				<input type="text" id="inputPicklimit" class="input-mini" disabled="true" value="<?php echo t::out($B['vote']->v('PickLimit'));?>" placeholder="整数,>2">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputDatebegin">投票时间</label>
			<div class="controls">
				投票将于<input type="text" id="inputDatebegin" value="<?php echo date('Y-m-d',$B['vote']->v('BeginTime'));?>" class="input-small" placeholder="例:2000-01-01格式">开始
				并于<input type="text" id="inputDaylast" class="input-mini" value="<?php echo t::out($B['vote']->v('DayLast'));?>" placeholder="1~30">天后结束
			</div>
		</div>
		<div class="control-group">
			<div class="controls">
				<button type="submit" id="add_vote_btn" class="btn btn-primary">
					保存
				</button>
				<button type="reset" class="btn">
					重置
				</button>
			</div>
		</div>
	</form>
</div>
<script>
$(document).on("click",'#option_list .remove_option',function(){
	$(this).parent().fadeOut();
	$(this).parent().remove();
	if($('#option_list li').length < 10){
		$("#add_option_box").show();
	}
});

//添加投票选项
$("#add_option_box #add_option_btn").click(function(){
	var option_str = $.trim($("#add_option_box #inputAddoption").val());
	if(option_str!=''){
		if($('#option_list li').length < 10){
			if(option_str.lenth>80){
				art.dialog.alert("投票选项应为1~80个字符!",function(){
					$("#add_option_box #inputAddoption").focus();
				});
			}else{
				$("#option_list").append('<li><a href="javascript:;" class="remove_option">删除</a><span>'+
					$.trim($("#add_option_box #inputAddoption").val())+'</span></li>');
				$("#add_option_box #inputAddoption").val('').focus();
				if($('#option_list li').length > 9) $("#add_option_box").hide();
			}
		}
	}else{
		art.dialog.alert("投票选项应为1~80个字符!",function(){
			$("#add_option_box #inputAddoption").focus();
		});
	}
});

//保存投票
$('#add_vote_btn').click(function(){
	var vote = {};
	vote.id = <?php echo $B['vote']->v('Id');?>;
	vote.name = $.trim($('#add_vote_form #inputName').val());
	vote.description = $.trim($('#add_vote_form #inputDescription').val());
	vote.picklimit = $.trim($('#add_vote_form #inputPicklimit').val());
	vote.votetype = $('#add_vote_form :radio:checked').length == 1 ? $('#add_vote_form :radio:checked').val() : 0;
	vote.options = new Array();
	vote.datebegin = $.trim($('#add_vote_form #inputDatebegin').val());
	vote.daylast = $.trim($('#add_vote_form #inputDaylast').val());
	if(vote.name.length < 5 || vote.name.length > 30){
		art.dialog.alert("投票名称应为5~30个字符!",function(){
			$('#add_vote_form #inputName').focus();
		});
		return false;
	}
	if(vote.description.length > 200){
		art.dialog.alert("投票描述请限制在200字符内!",function(){
			$('#add_vote_form #inputDescription').focus();
		});
		return false;
	}
	if($('#option_list li').length == 0){
		art.dialog.alert("您还没有添加投票选项!",function(){
			$('#add_vote_form #inputAddoption').focus();
		});
		return false;
	}else{
		$('#option_list li').each(function(){
			vote.options.push($('span',this).text());
		});
	}
	if(vote.votetype != 0 && (!/^[23456789]|[10]$/.test(vote.picklimit) || parseInt(vote.picklimit)>$('#option_list li').length)){
		art.dialog.alert("多选项数应该大于2并且小于现有选项数!",function(){
			$('#add_vote_form #inputPicklimit').focus();
		});
		return false;
	}
	if(vote.datebegin == ''){
		art.dialog.alert("您需要填写投票开始的日期!",function(){
			$('#add_vote_form #inputDatebegin').focus();
		});
		return false;
	}
	if(!/^[12]\d{0,1}|30|[3456789]$/.test(vote.daylast) || parseInt(vote.daylast)<1 || parseInt(vote.daylast)>30){
		art.dialog.alert("投票时间应该在1到30天!",function(){
			$('#add_vote_form #inputDaylast').focus();
		});
		return false;
	}
	
	$.post(
		'<?php echo BASE_URL?>/?c=vote&a=edit&op=ajax&rand='+Math.random(),
		vote,
		function(data){
			if(data.succeed == 1){
				art.dialog.tips("修改投票成功!");
				setTimeout(function(){window.location.href="<?php echo BASE_URL;?>/?c=vote&a=view&id="+data.data+"&rand=<?php echo rand();?>";},1500);
			}else{
				art.dialog.alert(data.msg);
			}
		},
		'json'
	)
});

$('#add_vote_form :radio').click(function(){
	$("#inputPicklimit").attr('disabled',$(this).val()==0?true:false);
});
</script>
<?php else:?>
<h3><?php echo implode("<br />",$B['error']);?></h3>
<?php endif;?>